package sy1and2;

import org.junit.Test;
import java.util.List;
import static org.junit.Assert.*;

public class ArithmeticQuestionGeneratorTest {

    // 测试加法题目生成
    @Test
    public void testGenerateAdditionQuestion() {
        List<String> questions = ArithmeticQuestionGenerator.generateQuestions("加法", false, 5);
        assertEquals(5, questions.size());
        for (String q : questions) {
            assertTrue(ArithmeticQuestionGenerator.isResultValid(q));
            assertTrue(q.contains("+"));
        }
    }

    // 测试减法题目生成
    @Test
    public void testGenerateSubtractionQuestion() {
        List<String> questions = ArithmeticQuestionGenerator.generateQuestions("减法", true, 3);
        assertEquals(3, questions.size());
        for (String q : questions) {
            assertTrue(ArithmeticQuestionGenerator.isResultValid(q));
            assertTrue(q.contains("-"));
        }
    }

    // 测试乘法题目生成
    @Test
    public void testGenerateMultiplicationQuestion() {
        List<String> questions = ArithmeticQuestionGenerator.generateQuestions("乘法", false, 4);
        assertEquals(4, questions.size());
        for (String q : questions) {
            assertTrue(ArithmeticQuestionGenerator.isResultValid(q));
            assertTrue(q.contains("×"));
        }
    }

    // 测试除法题目生成
    @Test
    public void testGenerateDivisionQuestion() {
        List<String> questions = ArithmeticQuestionGenerator.generateQuestions("除法", true, 2);
        assertEquals(2, questions.size());
        for (String q : questions) {
            assertTrue(ArithmeticQuestionGenerator.isResultValid(q));
            assertTrue(q.contains("÷"));
        }
    }

    // 测试加减混合运算生成
    @Test
    public void testGenerateAddSubMixedQuestion() {
        List<String> questions = ArithmeticQuestionGenerator.generateQuestions("加减混合", false, 3);
        assertEquals(3, questions.size());
        for (String q : questions) {
            assertTrue(ArithmeticQuestionGenerator.isResultValid(q));
            assertTrue((q.contains("+") || q.contains("-")) && !(q.contains("*") || q.contains("/")));
        }
    }

    // 测试乘除混合运算生成
    @Test
    public void testGenerateMulDivMixedQuestion() {
        List<String> questions = ArithmeticQuestionGenerator.generateQuestions("乘除混合", true, 3);
        assertEquals(3, questions.size());
        for (String q : questions) {
            assertTrue(ArithmeticQuestionGenerator.isResultValid(q));
            assertTrue((q.contains("×") || q.contains("÷")) && !(q.contains("+") || q.contains("-")));
        }
    }

    // 测试四则混合运算生成
    @Test
    public void testGenerateFourMixedQuestion() {
        List<String> questions = ArithmeticQuestionGenerator.generateQuestions("四则运算", true, 2);
        assertEquals(2, questions.size());
        for (String q : questions) {
            assertTrue(q.contains("×") || q.contains("÷"));
            assertTrue(q.contains("+") || q.contains("-"));
            assertTrue(ArithmeticQuestionGenerator.isResultValid(q));
        }
    }
    // 测试结果验证方法
    @Test
    public void testIsResultValid() {
        // 有效场景：加法（结果在0-100之间）
        assertTrue(ArithmeticQuestionGenerator.isResultValid("15 + 30 = "));
        // 有效场景：乘法（结果在0-100之间）
        assertTrue(ArithmeticQuestionGenerator.isResultValid("25 × 3 = "));
        // 有效场景：除法（结果在0-100之间且整除）
        assertTrue(ArithmeticQuestionGenerator.isResultValid("80 ÷ 4 = "));
        // 有效场景：混合运算（先乘除后加减，结果在0-100之间）
        assertTrue(ArithmeticQuestionGenerator.isResultValid("30 + 5 × 2 = "));

        // 无效场景：结果≥100
        assertFalse(ArithmeticQuestionGenerator.isResultValid("90 + 10 = "));
        // 无效场景：结果≤0
        assertFalse(ArithmeticQuestionGenerator.isResultValid("5 - 10 = "));
        // 无效场景：除法不整除
        assertFalse(ArithmeticQuestionGenerator.isResultValid("10 ÷ 3 = "));
    }
}